package com.rgp.servicedriveruser.service.impl;

import com.rgp.internalcommon.common.DriverInfoConstant;
import com.rgp.internalcommon.common.DriverVerificationConstant;
import com.rgp.internalcommon.common.DriverWorkStatusConstant;
import com.rgp.internalcommon.dto.ResponseResult;
import com.rgp.internalcommon.response.DriverInfoResponse;
import com.rgp.internalcommon.response.DriverVerificationResponse;
import com.rgp.servicedriveruser.mapper.DriverUserMapper;
import com.rgp.servicedriveruser.mapper.DriverUserWorkStatusMapper;
import com.rgp.servicedriveruser.pojo.DriverUser;
import com.rgp.servicedriveruser.pojo.DriverUserWorkStatus;
import com.rgp.servicedriveruser.service.DriverUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.List;

/**
 * @author nhp
 * @version 1.0
 * @description TODO
 * @date 2024-04-16 13:25
 */
@Service
public class DriverUserServiceImpl implements DriverUserService {

    @Autowired
    private DriverUserMapper driverUserMapper;

    @Autowired
    private DriverUserWorkStatusMapper driverUserWorkStatusMapper;

    @Override
    public ResponseResult selectByDriverUserId(Long id) {
        DriverUser driverUser = driverUserMapper.selectById(id);
        return ResponseResult.success(driverUser);
    }

    @Transactional
    @Override
    public void addDriverUser(DriverUser driverUser) {
        //1.新增司机
        driverUserMapper.insert(driverUser);

        //2.新增对应的司机工作状态记录
        DriverUserWorkStatus statusEntity = new DriverUserWorkStatus();
        statusEntity.setDriverId(driverUser.getId());
        statusEntity.setWorkStatus(DriverWorkStatusConstant.STOP);
        driverUserWorkStatusMapper.insert(statusEntity);
    }

    @Override
    public Integer updateDriverUser(DriverUser driverUser) {
        return driverUserMapper.updateById(driverUser);
    }

    @Override
    public DriverVerificationResponse getDriverByPhoneNum(String phondNum) {

        DriverVerificationResponse response = new DriverVerificationResponse();
        response.setPhoneNum(phondNum);
        response.setIsExists(DriverVerificationConstant.EXIST);

        //1.构造查询条件
        HashMap<String, Object> map = new HashMap<>();
        map.put("driver_phone",phondNum);
        map.put("state", DriverInfoConstant.VALID_STATE);

        //2.开始查询
        List<DriverUser> driverUsers = driverUserMapper.selectByMap(map);

        if (driverUsers == null || driverUsers.size() == 0){
            response.setIsExists(DriverVerificationConstant.NOT_EXIST);
        }else{
            String driverName = driverUsers.get(0).getDriverName();
            response.setDriverName(driverName);
        }
        return response;
    }

    @Override
    public ResponseResult<Boolean> judgeIfExistAvailableDrivers(String cityCode,Integer state) {
        Integer count = driverUserMapper.judgeIfExistAvailableDrivers(cityCode,state);
        if (count > 0){
            return ResponseResult.success(true);
        }
        return ResponseResult.success(false);
    }

    @Override
    public ResponseResult<DriverInfoResponse> getDriverInfoByDriverId(String driverId) {
        DriverUser driverUser = driverUserMapper.selectById(driverId);
        DriverInfoResponse infoResponse = new DriverInfoResponse();
        infoResponse.setDriverPhone(driverUser.getDriverPhone());
        infoResponse.setLicenseId(driverUser.getLicenseId());
        return ResponseResult.success(infoResponse);
    }
}